package com.app.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by zxy on 2021/3/25
 * 响应工具
 */
public class ResponseUtil {
    /**
     * 发送信息
     *
     * @param res
     * @param message
     */
    public static void sendMessage(HttpServletResponse res, Integer code, String message, String token) {
        PrintWriter out = null;
        try {
            res.setContentType("application/json;charset=utf-8");
            res.setStatus(code);
            res.setHeader("Access-Control-Allow-Origin", "*");
            out = res.getWriter();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code", code);
            map.put("message", message);
            if (token != null){
                map.put("token",token);
            }
            out.write(JSON.toJSONString(map));
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }
}
